package com.yanqu.road.server.cmd.activity.twins.pvp;

import com.yanqu.road.entity.activity.ActivityConditionInfo;
import com.yanqu.road.entity.activity.peakstronghold.PeakStrongHoldGuessValueArgs;
import com.yanqu.road.entity.activity.twins.config.TwinsConfig;
import com.yanqu.road.entity.enums.eGamePlayerEventType;
import com.yanqu.road.entity.task.args.UnionRiseRankArgs;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.TwinsProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.NormalActivityMgr;
import com.yanqu.road.server.manger.activity.peakstronghold.PeakStrongholdActivityMgr;
import com.yanqu.road.server.manger.activity.twins.TwinsMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.utils.string.StringUtils;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Cmd(code = GameProtocol.S_TWINS_CROSS_CONDITION_SYNC, desc = "跨服要下来同步condition可以都用这个")
public class TwinsCrossConditionSyncCmd extends AbstractCommand {

    TwinsProto.SimpleCrossConditionSyncMsg reqMsg;
    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        reqMsg = TwinsProto.SimpleCrossConditionSyncMsg.parseFrom(packet.getBytes());
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        if(PeakStrongholdActivityMgr.getConfig() != null &&
                ActivityMgr.activityInShowTime(PeakStrongholdActivityMgr.getConfig().getActivityInfo()) &&
                PeakStrongholdActivityMgr.getConfig().getActivityInfo().getActivityId() == reqMsg.getActivityId() ){
            for(TwinsProto.SimpleCrossConditionMsg crossSyncMsg : reqMsg.getSyncDataList()){
                if(crossSyncMsg.getUserId() == 0){
                    for(GamePlayer playerOnline : GamePlayerMgr.getAllOnlinePlayer()){
                        peakStrongHoldNotice(playerOnline.getUserId(),crossSyncMsg);
                    }
                }else{
                    peakStrongHoldNotice(crossSyncMsg.getUserId(),crossSyncMsg);
                }
            }
            return;
        }
        TwinsConfig config = TwinsMgr.getConfig();
        if (config == null || !ActivityMgr.activityInShowTime(config.getActivityInfo()) || config.getActivityInfo().getActivityId() != reqMsg.getActivityId()) {
            ret = GameErrorCode.E_ACTIVITY_NO_FOUND;
            return;
        }
        for(TwinsProto.SimpleCrossConditionMsg crossSyncMsg : reqMsg.getSyncDataList()){
            GamePlayer gamePlayer = GamePlayerMgr.getOnlinePlayer(crossSyncMsg.getUserId());
            if(gamePlayer != null){
                if(crossSyncMsg.getType() == eGamePlayerEventType.TwinsUserScoreRank.getValue()){
                    gamePlayer.notifyListener(eGamePlayerEventType.TwinsUserScoreRank.getValue(), new BigInteger(crossSyncMsg.getValue()));
                    gamePlayer.notifyListener(eGamePlayerEventType.TwinsUnionScoreRank.getValue(),
                            new UnionRiseRankArgs(UnionRiseRankArgs.NOTIFY_TYPE_NORMAL, new BigInteger(crossSyncMsg.getValue()), gamePlayer.getUserInfo().getUnionUid()));
                }
            }
        }
    }

    private void peakStrongHoldNotice(long userId,TwinsProto.SimpleCrossConditionMsg crossSyncMsg){
        GamePlayer gamePlayer = GamePlayerMgr.getOnlinePlayer(userId);
        if(gamePlayer != null){
            //个人伤害同步
            if(crossSyncMsg.getType() == eGamePlayerEventType.PeakStrongHoldUserDamageRank.getValue()){
                gamePlayer.notifyListener(eGamePlayerEventType.PeakStrongHoldUserDamageRank.getValue(), new BigInteger(crossSyncMsg.getValue()));
            }
            //第一轮竞猜
            if(crossSyncMsg.getType() == eGamePlayerEventType.PeakStrongHoldGuessWin.getValue()){
                List<String> tempStringList = StringUtils.stringToStringList(crossSyncMsg.getParam(),"\\|");
                BigInteger timeId = new BigInteger(tempStringList.get(0));
                Map<Integer,Integer> guessNumMap = new ConcurrentHashMap<>();
                //判断最高的那个去触发
                List<ActivityConditionInfo> conditionInfoList = NormalActivityMgr.getActivityConditionInfoList(reqMsg.getActivityId());
                for(ActivityConditionInfo activityConditionInfo : conditionInfoList){
                    List<BigInteger> paramList = activityConditionInfo.getParamList();
                    if(paramList.get(2).equals(timeId)){
                        guessNumMap.put(activityConditionInfo.getConditionId(),paramList.get(3).intValue());
                    }
                }
                int maxGuessCount = 0;
                int maxConditionId = 0;
                int rightCount = Integer.valueOf(crossSyncMsg.getValue());
                for(int conditionId : guessNumMap.keySet()){
                    int guessCount = guessNumMap.get(conditionId);
                    if(guessCount >= maxGuessCount && rightCount >= guessCount){
                        maxGuessCount = guessCount;
                        maxConditionId = conditionId;
                    }
                }
                gamePlayer.notifyListener(eGamePlayerEventType.PeakStrongHoldGuessWin.getValue(),
                        new PeakStrongHoldGuessValueArgs(new BigInteger(crossSyncMsg.getValue()),crossSyncMsg.getParam(),maxConditionId));
            }
            //第二轮竞猜
            if(crossSyncMsg.getType() == eGamePlayerEventType.PeakStrongHoldGuessChampion.getValue()){
                gamePlayer.notifyListener(eGamePlayerEventType.PeakStrongHoldGuessChampion.getValue(),
                        new PeakStrongHoldGuessValueArgs(new BigInteger(crossSyncMsg.getValue()),crossSyncMsg.getParam(),0));
            }
        }
    }

    @Override
    public void sendData(GamePlayer player) {
        if (ret == 0) {
            return;
        }
    }
}
